package homework;

import java.util.ArrayList;

public class work04 {

	public static void main(String[] args) {
		getMaxSubstr("dajlskldjsalkdjsalkda","djaskldjlsalk");
		getMaxSubstr("abcdesgbva","abcd890gbva0abcd");
		getMaxSubstr("8888","09");
	}

	public static void getMaxSubstr(String m,String n){
		if(m==n){
			System.out.println(m);
			return;
		}
		int maxLen = 0;
		String small = m.length()>n.length()?n : m;
		String longer = m.length()>n.length()?m : n;
		ArrayList<String> list = new ArrayList<String>();
		for(int i = 0;i<small.length();i++){
			for(int j = small.length()-1;j>=i;j--){
				if(longer.contains(small.substring(i, j+1))){
					if(j-i+1 == maxLen && !list.contains(small.substring(i, j+1))){
						list.add(small.substring(i, j+1));
					}else if(j-i+1 > maxLen){
						list.clear();
						list.add(small.substring(i, j+1));
						maxLen = j-i+1;
					}										
				}
			}
		}
		System.out.println(list);
	}
}
